package com.bw.controller;

import com.bw.bean.User;
import com.bw.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("/tologin.do")
    public String toLogin(){
        return "login";
    }

    @RequestMapping("/login.do")
    public String login(User user, HttpSession session){
        User u=userService.login(user);
        if(u !=null){
            session.setAttribute("user",u);
            if(u.getLev()==0){
               return "redirect:pList.do";
            }else{
                // 管理员页面
                return "redirect:mList.do";
            }
        }else {
            return "login";
        }
    }


    // 普通用户
    @RequestMapping("/pList.do")
    public String pList(@RequestParam(value="cpage",defaultValue = "1") Integer cpage, Model model,HttpSession session){
        User  user = (User)session.getAttribute("user");
        PageHelper.startPage(cpage,3);
        List list=userService.pList(user.getUid());
        PageInfo pi = new PageInfo(list);
        model.addAttribute("pList",list);
        model.addAttribute("pi",pi);
        return "pList";
    }

    // 管理员页面
    @RequestMapping("/mList.do")
    public String mList(@RequestParam(value="cpage",defaultValue = "1") Integer cpage,String mohu1,Integer fid,Integer did,Model model){
        Map map=new HashMap();
        map.put("mohu1",mohu1);
        map.put("fid",fid);
        map.put("did",did);
        PageHelper.startPage(cpage,3);
        List list=userService.mList(map);
        PageInfo pi = new PageInfo(list);
        model.addAttribute("pi",pi);
        model.addAttribute("list",list);
        return "mList";
    }

    @RequestMapping("/toChange.do")
    public String toChange(){
        return "change";
    }

    // 学生列表
    @RequestMapping("/userList.do")
    @ResponseBody
    public Object userList(){
        List list=userService.userList();
        return list;
    }
   // 宿舍楼类表
    @RequestMapping("/floorList.do")
    @ResponseBody
    public List floorList(){
        List list=userService.floorList();
        return list;
    }

    @RequestMapping("/dorList.do")
    @ResponseBody
    public Object dorList(Integer fid){
        List list=userService.dorList(fid);
        return list;
    }
    @RequestMapping("valid.do")
    @ResponseBody
    public Object valid(Integer did){
        Map map=userService.valid(did);
        return map;
    }

    @RequestMapping("/change.do")
    public String  change(Integer uid,Integer did){
        //map:uid  用户id  fid是宿舍楼 id   did 是  屋id
        Map map=new HashMap();
        map.put("uid",uid);
        map.put("did",did);
        int i=userService.updchagne(map);
        return "redirect:mList.do";
    }
}
